<?php
/**
  *
  *广告管理
  *
  *
  */
class AdAction extends Action
{
	public function preMethod()
	{
		R("Mayijie/check_admin");
		$perm_key = session('admin_perm');
		$perm_arr = explode(",",$perm_key);
		if(!in_array(2,$perm_arr)){
			$this->error("无权操作");
		}
	}

	//广告位
	public function ad_place(){
		$Place=M('Ads_place');
		$action=$this->_get('action');
		switch($action){
			case 'show':
				$this->place=$Place->order('place_tag')->select();
				$this->display();
				break;
			case 'add':
				$map['place_tag']=$this->_post('ad_tag');
			    if($Place->where($map)->select()){$this->error("广告位标示符已被占用！");}
				$data['place_tag']=$map['place_tag'];
				$data['place_name']=$this->_post('place');
				$data['type']=$this->_post('type');
				if($Place->add($data)){
					$this->success("添加成功！");
				}
			    break;
			case 'update':
				$map['place_id']=$this->_post('place_id');
			    $data['place_name']=$this->_post('place');
				$data['type']=$this->_post('type');
				if($Place->where($map)->save($data)){
					$this->success("修改成功！");
				}
			    break;
		}
	}

	public function ad(){
		$Ad=M('Ads');
		$map['place_tag']=$this->_get('place_tag');
		$this->adlist=$Ad->where($map)->order('sorting asc')->select();
		$this->display();
	}
    
	//添加广告
	public function add_ad(){
		$Place=M('Ads_place');
		$map['place_tag']=$this->_get('place_tag');
		$map['type']=$this->_get('type');

		$this->place=$Place->where($map)->find();
		$this->display();
	}
    //广告插入
	public function insert(){
		$Ad=M('Ads');
		$data['place_tag']=$this->_post('place_tag');
		$data['is_show']=$this->_post('is_show');
		$data['end_time']=$this->_post('end_time')==0?strtotime('2020-01-01'):strtotime($this->_post('end_time'));
		$data['add_time']=time();
		$data['type']=$this->_post('type');

		switch($data['type']){/*}}}*/
			case '1':
				$data['content']=stripslashes($_POST['content']);
			    if($Ad->add($data)){
					echo "添加成功√";
				}else{
					echo "<font color='red'>添加失败X！</font>";
				}
			break;
			case '2':
				$chang=$this->_post('chang');
			    $kuan=$this->_post('kuan');
				$new_win=$this->_post('new_win');
				if(!empty($chang)){
					$width="width='".$chang."px' ";
				}
				if(!empty($kuan)){
					$height="height='".$kuan."px'";
				}
				if(!empty($new_win)){
					$new_win=" target='_blank'";
				}
				
				$data['path_url']=$this->_post('url');
				if($data['path_url']=='http://'){
					$data['path_url']="#";
				}
				$pic=$this->pic_upload($data['place_tag']);
				for($i=0;$i<count($pic);$i++){
					$data['file']=$pic[$i]['savepath'].$pic[$i]['savename'];
					$data['content']="<a href='{$data['path_url']}'{$new_win}><img src='__APP__/{$data['file']}' {$width}{$height}></a>";
					if($Ad->add($data)){
						echo "添加成功√";
					}else{
						@unlink($data['file']);
						echo "<font color='red'>添加失败X！</font>";
					}
				}
				$this->writeAd();
				$this->success("添加成功！");
			break;
			case '3':
				if($data['path_url']=='http://'){
					$data['path_url']="#";
				}
				$flash=$this->flash_upload();
			break;
			case '4':
				$data['content']=stripslashes($_POST['content']);
			    if($Ad->add($data)){
					echo "添加成功√";
				}else{
					echo "<font color='red'>添加失败X！</font>";
				}
			break;
		}/*}}}*/
	}

	public function writeAd(){
		$Ad=M('Ads');
		ob_start(); //打开缓冲区  
		$ad01=$Ad->where("place_tag='SearchR'")->order('sorting desc')->find();
		echo $ad01['content'];
		$searchr=ob_get_contents(); 
		file_put_contents('./core/Modules/Shop/Tpl/default/Ad/SearchR.html',$searchr);
		ob_end_clean();

	}

	private function pic_upload($dir) {
		foreach($_FILES as $k=>$v){
			if (!empty($v['name'])) {
				return $pic=$this->pic_up($dir);
			}
		}
    }

    private function pic_up($dir) {
        import('ORG.Net.UploadFile');
        $upload = new UploadFile();
        $upload->maxSize            = 3292200;
        $upload->allowExts          = explode(',', 'jpg,gif,png,jpeg'); 
		$upload->savePath           = './ad/'.$dir.'/';
        //$upload->thumb              = true;
        $upload->imageClassPath     = 'ORG.Util.Image';
        //$upload->thumbPrefix        = 'a';  //生产1张缩略图
        //$upload->thumbMaxWidth      = '1200';
        //$upload->thumbMaxHeight     = '500';
        $upload->saveRule           = 'uniqid';
        $upload->thumbRemoveOrigin  = true;
		
        if (!$upload->upload()) {
            $this->error($upload->getErrorMsg());
        } else {
            return $upload->getUploadFileInfo();
        }
    }

	//更改是否显示
	public function change_show(){
		$Ad=M('Ads');
		$action=$this->_get('action');
		$map['ad_id']=$this->_get('ad_id');
		switch($action){
			case 'hidden':
				$data['is_show']='false';
			    $Ad->where($map)->save($data);
			break;
			case 'show':
				$data['is_show']='true';
			    $Ad->where($map)->save($data);
			break;
		}
		echo '0';
	}

	//更改排序、url
	public function save_sort(){
		$Ad=M('Ads');
		$ads=$this->_post('adid');
		$sort=$this->_post('sorting');
		$url=$this->_post('url');
		for($i=0;$i<count($ads);$i++){
			$data['sorting']=$sort[$i];
			$data['path_url']=$url[$i];
			$Ad->where('ad_id='.$ads[$i])->save($data);
		}
		$this->success("更改成功！");
	}
	
	//删除广告
	public function remove(){
		$Ad=M('Ads');
		$map['ad_id']=$this->_get('ad_id');
		$ad=$Ad->where($map)->find();

		if($ad['type']=='2'||$ad['type']=='3'){
		   if(@unlink($ad['file'])){
			   $Ad->where($map)->delete();
			   echo 1;
		   }else if(!file_exists($ad['file'])){
			   $Ad->where($map)->delete();
			   echo 1;
		   }else{
			   echo 0;
		   }
		}else{
		   $Ad->where($map)->delete();
		   echo 1;
		}
	}

	public function place_edit(){
		$Place=M('Ads_place');
		$map['place_id']=$this->_get('id');
		$this->place=$Place->where($map)->find();
		$this->display();
	}

}
?>

